package cloud.dwz.service.impl;

import cloud.dwz.entity.AuthEntity;
import cloud.dwz.service.AuthService;
import cloud.dwz.tool.ToolJWT;
import io.wit.modules.dwz.AuthDao;
import io.wit.modules.sys.shiro.ShiroUtils;
import io.wit.tool.ToolClient;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Service
public class AuthServiceImpl implements AuthService{

    @Resource
    private AuthDao authDao;

    @Value("${register_dafualt_role}")
    private Long dafualtRole;

    @Override
    public String authLogin(final HttpServletRequest request){
        final String filter = filterRequired(request);
        if(filter != null)return filter;
        final String userName = request.getParameter("userName");
        final String password = request.getParameter("password");
        final AuthEntity authEntity = authDao.getUserInfo(userName);
        if(authEntity == null){
            return ToolClient.createJsonFail("用户名或密码错误");
        }
        final String sha256 = ShiroUtils.sha256(password,authEntity.getSalt());
        if(authEntity.getPassword().equals(sha256)){
            final Integer status = authEntity.getStatus();
            switch (status){
                case 0:
                    return ToolClient.createJsonFail("账号已被禁用!");
                case 2:
                    return ToolClient.createJsonFail("账号待审核,请联系管理员!");
                case 3:
                    return ToolClient.createJsonFail("账号服务到期!");
                default:
                    break;
            }
            final HashMap<String,Object> map = new HashMap<>();
            final Long employeeRoleId = authEntity.getEmployeeRoleId();
            int userType = 1;
            if(employeeRoleId != null){
                userType = 2;
            }
            final String token = ToolJWT.expireAccessToken(authEntity.getUserId(),userType);
            map.put("userName",userName);
            map.put("token",token);
            map.put("flag",authEntity.getFlag());
            map.put("roleId",authEntity.getRoleId());
            map.put("employeeRoleId",employeeRoleId);
            return ToolClient.queryJson(map);
        }else{
            return ToolClient.createJsonFail("用户名或密码错误!");
        }
    }

    //验证必填
    private String filterRequired(final HttpServletRequest request){
        final String userName = request.getParameter("userName");
        if(userName == null || userName.length() <= 0){
            return ToolClient.createJsonFail("请输入用户名!");
        }
        final String password = request.getParameter("password");
        if(password == null || password.length() <= 0){
            return ToolClient.createJsonFail("请输入登录密码!");
        }
        return null;
    }

    //先注册用户,后期再完善个人资料及基地信息
    @Override
    @Transactional
    public String register(final HttpServletRequest request){
        final String filter = filterRequired(request);
        if(filter != null)return filter;
        final String userName = request.getParameter("userName");
        final String password = request.getParameter("password");
        final String verify = request.getParameter("verify");
        if(verify == null || verify.length() <= 0){
            return ToolClient.createJsonFail("请输入确认密码!");
        }
        if(!password.equals(verify)){
            return ToolClient.createJsonFail("输入的两次密码不一致!");
        }
        final Long exist = authDao.existUser(userName);
        if(exist != null){
            return ToolClient.createJsonFail("该手机号已经注册,请登录!");
        }
        final HashMap<String,Object> params = new HashMap<>();
        final String salt = RandomStringUtils.randomAlphanumeric(20);
        final String sha256 = ShiroUtils.sha256(password,salt);
        params.put("username",userName);
        params.put("password",sha256);
        params.put("salt",salt);
        final int rows = authDao.register(params);
        final HashMap<String,Object> roles = new HashMap<>();
        roles.put("user_id",params.get("user_id"));
        roles.put("role_id",dafualtRole);
        authDao.addUserRole(roles);
        return ToolClient.executeRows(rows,"注册成功,感谢使用!","注册失败,稍候重试!");
    }
}